package com.salinity.kun.algorithm.harmonic;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.salinity.kun.util.DateUtil;

/**
 * 平均黄经
 * 
 * @author Administrator
 *
 */
public class AvgEclipticLongitude {

	// 前五个分别指s0,h0,p0,N0,p01 对应：平台应、平太阳、近地点、升交点、近日点等的平均黄经
	public static enum ELSymbol {
		s, h, p, N, p1, t1
	};

	private LocalDateTime LDTime;
	private Map<ELSymbol, Double> cacheMap;

	public AvgEclipticLongitude(Date date) {
		LDTime = DateUtil.Date2LDTime(date);
		cacheMap = new HashMap<>();
	}

	/**
	 * 
	 * @param eLSymbol
	 * @return
	 * 返回的是角度，而非弧度
	 */
	public double getAELongitude(ELSymbol eLSymbol) {
		int Y = LDTime.getYear();
		int n = LDTime.getDayOfYear() - 1;
		int i = (Y - 1901) / 4;
		int t = LDTime.getHour() + LDTime.getMinute() / 60;
		double days = (n + i + t / 24);
		double rslt = 0.0;
		if (!cacheMap.containsKey(eLSymbol)) {
			switch (eLSymbol) {
			case s:
				rslt = 277.025 + 129.38481 * (Y - 1900) + 13.1764 * days;
				break;
			case h:
				rslt = 280.19 - 0.23872 * (Y - 1900) + 0.98565 * days;
				break;
			case p:
				rslt = 334.385 + 40.66249 * (Y - 1900) + 0.1114 * days;
				break;
			case N:
				rslt = 259.157 - 19.32818 * (Y - 1900) - 0.05295 * days;
				break;
			case p1:
				rslt = 281.221 + 0.01718 * (Y - 1900) + 0.0000471 * days;
				break;
			case t1:
				rslt = 15 * t - getAELongitude(ELSymbol.s) + getAELongitude(ELSymbol.h);
				break;
			default:
				break;
			}
			cacheMap.put(eLSymbol, rslt);
		}
		return cacheMap.get(eLSymbol);
	}
}
